package sunyu.demo.spring.hbase;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.data.hadoop.hbase.RowMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * hbase result 映射
 *
 * @author 孙宇
 */
public class HbaseRowMapper implements RowMapper {
    @Override
    public Map mapRow(Result result, int rowNum) throws Exception {
        Map row = new HashMap<>();

        row.put("rowKey", Bytes.toString(result.getRow()));//rowName

        for (Cell cell : result.rawCells()) {//所有列
            String family = Bytes.toString(CellUtil.cloneFamily(cell));// 获取当前列的familyName
            String column = Bytes.toString(CellUtil.cloneQualifier(cell));//列名
            String value = Bytes.toString(CellUtil.cloneValue(cell));//列值
            long timestamp = cell.getTimestamp();//插入时间

            Map familyMap = (Map) row.get(family);
            if (familyMap == null) {
                row.put(family, new HashMap<>());
                familyMap = (Map) row.get(family);
            }
            familyMap.put(column, value);
            //familyMap.put(column + "_timestamp", timestamp);
        }

        return row;
    }
}
